﻿// Fuel - 油量表
#include "Fuel.h"

// 基准参数
const double DIAL_RADIUS_BASE = 180.0;
const double CENTER_X_BASE = 400.0;
const double CENTER_Y_BASE = 300.0;

void drawFUI(double centerX, double centerY, double dialRadius, double left,double right) {
    /*dialRadius = 100;
    centerX = 340;
    centerY = 330;*/

    //initgraph(800, 600);
    //setrendermode(RENDER_MANUAL);
    //setbkcolor(EGERGB(100, 100, 100));
    settextjustify(LEFT_TEXT, TOP_TEXT);

    double current_heading = 0;
    //for (; is_run(); delay_fps(60))
    //{
    //    cleardevice();
        current_heading = updateinputFQI(current_heading);//数据变化模拟
        screwFQI(centerX, centerY, dialRadius);//螺丝
        outerCircleFQI1(centerX, centerY, dialRadius);//外圈边框
        drawScaleFQI2(centerX, centerY, dialRadius);
        drawScaleFQI1(centerX, centerY, dialRadius);
        leftheadFQI(current_heading, centerX, centerY, dialRadius,left);
        rightheadFQI(current_heading, centerX, centerY, dialRadius,right);

        innerCircleFQI1(centerX, centerY, dialRadius);//内圈
        buttonFQI(centerX, centerY, dialRadius);//两边黑黑的半圆

        drawnumbersFQI(centerX, centerY, dialRadius);//字母



    //    getch();
    //}
    //closegraph();
    //return 0;

}

//模拟数据输入
double updateinputFQI(double current_heading) {
    static int direction = 1;
    current_heading += direction * 1.5;

    //if (current_heading >= 360) direction = -1;
    //if (current_heading <= 0) direction = 1;
    return current_heading;
}

void screwFQI(double centerX, double centerY, double dialRadius)
{
    // 使用float保证计算精度（根据需求可改为double）
    const float DIAL_RADIUS_F = (float)dialRadius;
    const float CENTER_X_F = (float)centerX;
    const float CENTER_Y_F = (float)centerY;

    // 参数化定义（精确到小数点后6位）
    const float SCREW_OFFSET = DIAL_RADIUS_F * 0.055555556f; // 1/18
    const float ELLIPSE_OFFSET_X = -DIAL_RADIUS_F * 0.111111111f; // -1/9
    const float ELLIPSE_OFFSET_Y = -DIAL_RADIUS_F * 0.166666667f; // -1/6
    const float ELLIPSE_SIZE = DIAL_RADIUS_F * 0.277777778f;  // 5/18
    const float RECT_OFFSET_X = DIAL_RADIUS_F * 0.222222222f;  // 2/9
    const float RECT_OFFSET_Y = DIAL_RADIUS_F * 0.333333333f;  // 1/3
    const float RECT_WIDTH = DIAL_RADIUS_F * 0.555555556f;   // 5/9
    const float RECT_HEIGHT = DIAL_RADIUS_F * 0.611111111f;  // 11/18
    const float CROSS_OFFSET1 = DIAL_RADIUS_F * 0.055555556f;  // 1/18
    const float CROSS_OFFSET2 = DIAL_RADIUS_F * 0.027777778f;  // 1/36
    const float LINE_WIDTH = DIAL_RADIUS_F * 0.016666667f;  // 1/60

    ege_enable_aa(TRUE);

    // 绘制函数（使用浮点坐标）
    auto draw_screw = [&](float cx, float cy) {
        // 精确到小数点后6位的渐变参数
        ege_setpattern_ellipsegradient(
            ege_point{ roundf(cx), roundf(cy) }, // 最终转整数
            EGEACOLOR(0xFF, BLACK),
            roundf(cx + ELLIPSE_OFFSET_X),
            roundf(cy + ELLIPSE_OFFSET_Y),
            roundf(ELLIPSE_SIZE),
            roundf(ELLIPSE_SIZE),
            EGEACOLOR(0, WHITE)
        );
        ege_fillrect(
            roundf(cx - RECT_OFFSET_X),
            roundf(cy - RECT_OFFSET_Y),
            roundf(RECT_WIDTH),
            roundf(RECT_HEIGHT)
        );

        // 灰色渐变层（精确尺寸调整）
        const float adjusted_size = ELLIPSE_SIZE - DIAL_RADIUS_F * 0.055555556f; // 替代原-10
        ege_setpattern_ellipsegradient(
            ege_point{
              roundf(cx - SCREW_OFFSET * 0.5f),
                roundf(cy - SCREW_OFFSET * 0.5f)
            },
            EGEACOLOR(0xFF, EGERGB(170, 170, 170)),
            roundf(cx + ELLIPSE_OFFSET_X),
            roundf(cy + ELLIPSE_OFFSET_Y),
            roundf(adjusted_size),
            roundf(adjusted_size),
            EGEACOLOR(0xFF, EGERGB(20, 20, 20))
        );
        ege_fillrect(
            roundf(cx - RECT_OFFSET_X),
            roundf(cy - RECT_OFFSET_Y),
            roundf(RECT_WIDTH),
            roundf(RECT_HEIGHT)
        );

        // 十字线（亚像素级精度）
        setcolor(EGERGB(30, 30, 30));
        setlinewidth(roundf(LINE_WIDTH));

        const float cross_y = cy - CROSS_OFFSET2 * 2.0f;
        ege_line(
            roundf(cx - CROSS_OFFSET1), roundf(cross_y),
            roundf(cx + CROSS_OFFSET1), roundf(cross_y)
        );

        const float cross_x = cx;
        const float cross_y_start = cy + CROSS_OFFSET2 / 4;
        const float cross_y_end = cy - CROSS_OFFSET2 * 4.0f;
        ege_line(
            roundf(cross_x), roundf(cross_y_start),
            roundf(cross_x), roundf(cross_y_end)
        );
        };

    // 计算四个螺丝中心坐标（浮点运算）
    const float screw_radius = DIAL_RADIUS_F - SCREW_OFFSET;

    // 左上
    draw_screw(
        CENTER_X_F - screw_radius,
        CENTER_Y_F - DIAL_RADIUS_F
    );

    // 右上
    draw_screw(
        CENTER_X_F + screw_radius,
        CENTER_Y_F - DIAL_RADIUS_F
    );

    // 右下
    draw_screw(
        CENTER_X_F + screw_radius,
        CENTER_Y_F + DIAL_RADIUS_F
    );

    // 左下（新增）
    draw_screw(
        CENTER_X_F - screw_radius,
        CENTER_Y_F + DIAL_RADIUS_F
    );
}
void innerCircleFQI1(double centerX, double centerY, double dialRadius) {
    // 计算比例因子（基于基准值 DIAL_RADIUS=180）
    const double scale = dialRadius / 180.0; // 基准半径为180，缩放比例

    // 启用抗锯齿
    ege_enable_aa(TRUE);

    // 线宽参数化（基准线宽3，与半径比例无关）
    setlinewidth(3.0);
    setcolor(EGERGB(40, 40, 40));

    // 内圆半径（基准半径减4，按比例缩放）
    const double innerRadius = dialRadius - 4.0 * scale; // 4是基准值，转换为当前半径的比例
    ege_circle(centerX, centerY, innerRadius);

    // 左右垂直线（坐标相对于圆心，基准偏移量100和146按比例缩放）
    const double offsetX = 100.0 * scale;  // 基准X偏移100
    const double offsetY = 146.0 * scale;  // 基准Y偏移146
    ege_line(
        centerX - offsetX, centerY + offsetY,
        centerX - offsetX, centerY - offsetY
    );
    ege_line(
        centerX + offsetX, centerY + offsetY,
        centerX + offsetX, centerY - offsetY
    );
}

//圆盘外框
void outerCircleFQI1(double CENTER_X, double CENTER_Y, double DIAL_RADIUS)
{
    ege_enable_aa(TRUE);  // 启用抗锯齿
    // 绘制圆环主体

        //setcolor(BLACK);//描边颜色
        //setlinewidth(15);//线条粗细
        //ege_circle(circle_x, circle_y, circle_r + circle_r / 10);//描边
    float circle_x = CENTER_X;
    float  circle_r = DIAL_RADIUS + 15 * DIAL_RADIUS / 180;
    float  circle_y = CENTER_Y;
    ege_point p5 = { circle_x - circle_r - circle_r / 10 + circle_r / 30 * 11 - circle_r / 4 * 1.4 ,circle_y - circle_r - circle_r / 10 + circle_r / 30 * 11 + circle_r / 2 * 1.5 };
    ege_setpattern_ellipsegradient(p5, EGERGB(190, 190, 190), circle_x - circle_r - circle_r / 10, circle_y - circle_r - circle_r / 10, 2 * (circle_r + circle_r / 10), 2 * (circle_r + circle_r / 10), EGERGB(30, 30, 30));
    ege_fillrect(circle_x - circle_r - circle_r / 10, circle_y - circle_r - circle_r / 10, 2 * (circle_r + circle_r / 10), 2 * (circle_r + circle_r / 10));

    //最外圈

    {
        ege_point p6 = { circle_x + circle_r + circle_r / 300 * 2 - circle_r / 30 * 10 ,circle_y - circle_r - circle_r / 300 * 2 + circle_r / 30 * 19 };
        ege_setpattern_ellipsegradient(p6, EGERGB(195, 195, 195), circle_x - circle_r - circle_r / 300 * 13, circle_y - circle_r - circle_r / 300 * 13, 2 * (circle_r + circle_r / 300 * 13), 2 * (circle_r + circle_r / 300 * 13), EGEACOLOR(0xFF, 0x373533));
        ege_fillrect(circle_x - circle_r - circle_r / 300 * 13, circle_y - circle_r - circle_r / 300 * 13, 2 * (circle_r + circle_r / 300 * 13), 2 * (circle_r + circle_r / 300 * 13));
        setlinewidth(8);//线条粗细
        setcolor(EGEACOLOR(0xFF, 0x1B1916));
        ege_circle(circle_x, circle_y, circle_r + circle_r / 25);
    }//第二圈


    setlinewidth(25);  // 设置线宽模拟圆环厚度
    setcolor(EGERGB(20, 20, 20));
    // ege_circle(CENTER_X, CENTER_Y, DIAL_RADIUS + 20);

    setlinewidth(15);  // 设置线宽模拟圆环厚度
    setcolor(EGERGB(55, 55, 55));
    //ege_circle(CENTER_X, CENTER_Y, DIAL_RADIUS + 15);


     // 绘制内部填充圆
    setfillcolor(EGERGB(65, 65, 65));
    ege_fillcircle(CENTER_X, CENTER_Y, DIAL_RADIUS - 5);
}


// 绘制左边刻度
void drawScaleFQI1(double centerX, double centerY, double dialRadius) {
    double scale = dialRadius / DIAL_RADIUS_BASE;
    setcolor(EGERGB(210, 210, 210)); // 刻度颜色白
    ege_enable_aa(TRUE);  // 启用抗锯齿
    setlinewidth(5 * scale); // 大刻度粗5

    // 绘制弧线
    double arcX = centerX - 220 * scale;
    double arcY = centerY - 100 * scale;
    double arcWidth = 200 * scale;
    double arcHeight = 200 * scale;
    double startAngle = 270 + 25.714;
    double endAngle = 25.7 * 5 - 3;
    ege_arc(arcX, arcY, arcWidth, arcHeight, startAngle, endAngle);

    // 绘制刻度线
    for (double i = 0; i < 360; i += 25.71428) {

        if (i < 170 && i > 10) {
            if (i > 150 && i < 170)
            {
                setcolor(EGERGB(168, 57, 61));
                double angle = i * M_PI / 180 - M_PI / 2;
                double r1 = (DIAL_RADIUS_BASE - 95) * scale;
                double r2 = (DIAL_RADIUS_BASE - 65) * scale;
                double x1 = centerX - 120 * scale + r1 * cos(angle);
                double y1 = centerY + r1 * sin(angle);
                double x2 = centerX - 120 * scale + r2 * cos(angle);
                double y2 = centerY + r2 * sin(angle);
                ege_line(x1, y1, x2, y2);
            }
            else
            {
                setcolor(EGERGB(210, 210, 210)); // 刻度颜色白
                double angle = i * M_PI / 180 - M_PI / 2;
                double r1 = (DIAL_RADIUS_BASE - 95) * scale;
                double r2 = (DIAL_RADIUS_BASE - 65) * scale;
                double x1 = centerX - 120 * scale + r1 * cos(angle);
                double y1 = centerY + r1 * sin(angle);
                double x2 = centerX - 120 * scale + r2 * cos(angle);
                double y2 = centerY + r2 * sin(angle);
                ege_line(x1, y1, x2, y2);
            }
        }


    }
}

// 绘制右边刻度
void drawScaleFQI2(double centerX, double centerY, double dialRadius) {
    double scale = dialRadius / DIAL_RADIUS_BASE;
    setcolor(EGERGB(210, 210, 210)); // 刻度颜色白
    ege_enable_aa(TRUE);  // 启用抗锯齿
    setlinewidth(5 * scale); // 大刻度粗5

    // 绘制弧线
    double arcX = centerX + 20 * scale;
    double arcY = centerY - 100 * scale;
    double arcWidth = 200 * scale;
    double arcHeight = 200 * scale;
    double startAngle = 119.714;
    double endAngle = 25.7 * 5 - 1;
    ege_arc(arcX, arcY, arcWidth, arcHeight, startAngle, endAngle);

    // 绘制刻度线
    for (double i = 0; i < 360; i += 25.71428) {
        if (i < 170 && i > 10) {
            if (i > 10 && i < 37)
            {
                setcolor(EGERGB(168, 57, 61)); // 刻度颜色白
                double angle = i * M_PI / 180 + M_PI / 2 + M_PI / 180 * 5;
                double r1 = (DIAL_RADIUS_BASE - 95) * scale;
                double r2 = (DIAL_RADIUS_BASE - 65) * scale;
                double x1 = centerX + 120 * scale + r1 * cos(angle);
                double y1 = centerY + r1 * sin(angle);
                double x2 = centerX + 120 * scale + r2 * cos(angle);
                double y2 = centerY + r2 * sin(angle);
                ege_line(x1, y1, x2, y2);
            }
            else
            {
                setcolor(EGERGB(210, 210, 210));
                double angle = i * M_PI / 180 + M_PI / 2 + M_PI / 180 * 5;
                double r1 = (DIAL_RADIUS_BASE - 95) * scale;
                double r2 = (DIAL_RADIUS_BASE - 65) * scale;
                double x1 = centerX + 120 * scale + r1 * cos(angle);
                double y1 = centerY + r1 * sin(angle);
                double x2 = centerX + 120 * scale + r2 * cos(angle);
                double y2 = centerY + r2 * sin(angle);
                ege_line(x1, y1, x2, y2);
            }
        }
    }
}

void drawnumbersFQI(double centerX, double centerY, double dialRadius) {
    // 计算缩放比例（基于基准半径 180）
    const double scale = dialRadius / 180.0;

    // 基础样式
    setcolor(EGERGB(210, 210, 210));
    ege_enable_aa(TRUE);
    setbkmode(TRANSPARENT);

    //------------------------ 字体尺寸参数化 ------------------------
    // 基准字体尺寸与半径的比例（可根据实际效果调整比例系数）
    setfont(40 * scale, 22 * scale, "MS UI Gothic");  // 基准字体大小 40:22
    setfont(18 * scale, 11 * scale, "黑体");           // 基准字体大小 18:11

    //------------------------ 文本坐标参数化（相对于圆心）------------------------
    auto draw_text = [&](double x_off, double y_off, const char* text, double font_scale) {
        // 计算相对于圆心的坐标（偏移量 * 缩放比例）
        const double x = centerX + x_off * scale;
        const double y = centerY + y_off * scale;
        setfont(font_scale * 18, font_scale * 11, "黑体");  // 基准字体为18:11，可动态调整比例
        outtextxy(x, y, text);
        };

    //------------------------ 具体文本绘制 ------------------------
    // 文本 "GALLONS"（基准偏移：-38, +100）
    draw_text(-38.0, 100.0, "GALLONS", dialRadius / 180.0);

    // 数字 "0"（基准偏移：-57, +95 和 +40, +95）
    setfont(33 * scale, 13 * scale, "MS UI Gothic");
    outtextxy(centerX - 57.0 * scale, centerY + 95.0 * scale, "0");
    outtextxy(centerX + 40.0 * scale, centerY + 95.0 * scale, "0");

    // 数字 "5"（基准偏移：-22, +60 和 +5, +60）
    outtextxy(centerX - 22.0 * scale, centerY + 60.0 * scale, "5");
    outtextxy(centerX + 5.0 * scale, centerY + 60.0 * scale, "5");

    // 数字 "10/15/20"（基准偏移：-17, -17/-57/-95）
    outtextxy(centerX - 17.0 * scale, centerY - 17.0 * scale, "10");
    outtextxy(centerX - 17.0 * scale, centerY - 57.0 * scale, "15");
    outtextxy(centerX - 17.0 * scale, centerY - 95.0 * scale, "20");
    double x1 = centerX + (-77.0) * scale;
    double y1 = centerY + (-132.0) * scale;
    double x2 = centerX + 56.0 * scale;
    double y2 = centerY + (-132.0) * scale;

    // 绘制文本（字体大小随比例缩放，此处假设基准字体为18pt）
    setfont(33 * scale, 13 * scale, "MS UI Gothic");
    outtextxy(x1, y1, "26");
    outtextxy(x2, y2, "26");
    // 文本 "QTY"（基准偏移：-40, +120）
    setfont(43 * scale, 28 * scale, "黑体");
    outtextxy(centerX - 40.0 * scale, centerY + 120.0 * scale, "QTY");

    // 文本 "FUEL"（基准偏移：-55, -170）
    outtextxy(centerX - 55.0 * scale, centerY - 170.0 * scale, "FUEL");

    // 垂直文本 "LEFT"（基准偏移：-150, -100/-56/-6/+37）
    setfont(32 * scale, 27 * scale, "黑体");
    outtextxy(centerX - 150.0 * scale, centerY - 95.0 * scale, "L");
    outtextxy(centerX - 150.0 * scale, centerY - 50.0 * scale, "E");
    outtextxy(centerX - 150.0 * scale, centerY - 1.0 * scale, "F");
    outtextxy(centerX - 150.0 * scale, centerY + 35.0 * scale, "T");

    // 垂直文本 "RIGHT"（基准偏移：+120, -120/-74/-22/+26/+70）
    outtextxy(centerX + 120.0 * scale, centerY - 115.0 * scale, "R");
    outtextxy(centerX + 120.0 * scale, centerY - 70.0 * scale, "I");
    outtextxy(centerX + 120.0 * scale, centerY - 22.0 * scale, "G");
    outtextxy(centerX + 120.0 * scale, centerY + 26.0 * scale, "H");
    outtextxy(centerX + 120.0 * scale, centerY + 70.0 * scale, "T");
}// 两边黑黑的半圆
void buttonFQI(double centerX, double centerY, double dialRadius) {
    double scale = dialRadius / DIAL_RADIUS_BASE;
    setcolor(EGERGB(40, 40, 40));
    ege_enable_aa(TRUE);  // 启用抗锯齿
    setfillcolor(EGERGB(40, 40, 40));
    double floodX1 = (278 - CENTER_X_BASE) * scale + centerX;
    double floodY1 = (193 - CENTER_Y_BASE) * scale + centerY;
    double floodX2 = (569 - CENTER_X_BASE) * scale + centerX;
    double floodY2 = (277 - CENTER_Y_BASE) * scale + centerY;
    floodfill(floodX1, floodY1, EGERGB(40, 40, 40));
    floodfill(floodX2, floodY2, EGERGB(40, 40, 40));
}
// 左边指针（参数化版本）
void leftheadFQI(double current_heading, double centerX, double centerY, double dialRadius,double left) {
    ege_enable_aa(TRUE);
    double CENTER_X = centerX;
    double CENTER_Y = centerY;
    double  DIAL_RADIUS = dialRadius;
    const double SCALE_MAIN_OFFSET = DIAL_RADIUS * 0.666667;  // 120/180
    const double POINTER_LENGTH = DIAL_RADIUS * 0.555556;      // 100/180
    const double POINTER_WIDTH = DIAL_RADIUS * 0.055556;       // 10/180
    const double ROOT_OFFSET = DIAL_RADIUS * 0.055556;         // 10/180
    // 参数计算
    const double baseX = CENTER_X - SCALE_MAIN_OFFSET;
    const double baseY = CENTER_Y;

    // 角度计算
    const double angle = 64-(left/2.744)*5;
    const double radian = angle * M_PI / 180.0;

    // 方向向量
    const double dirX = cos(radian);
    const double dirY = sin(radian);
    const double perpX = -dirY;
    const double perpY = dirX;

    // 顶点坐标计算
    ege_point points[4] = {
        { // 尖端
            round(baseX + POINTER_LENGTH * dirX),
            round(baseY + POINTER_LENGTH * dirY)
        },
        { // 根部左侧
            round(baseX - POINTER_WIDTH * perpX),
            round(baseY - POINTER_WIDTH * perpY)
        },
        { // 根部中心
            round(baseX - ROOT_OFFSET * dirX),
            round(baseY - ROOT_OFFSET * dirY)
        },
        { // 根部右侧
            round(baseX + POINTER_WIDTH * perpX),
            round(baseY + POINTER_WIDTH * perpY)
        }
    };

    setfillcolor(EGERGB(210, 210, 210));
    ege_fillpoly(4, points);
}

// 右边指针（参数化版本）
void rightheadFQI(double current_heading, double centerX, double centerY, double dialRadius,double right) {
    ege_enable_aa(TRUE);
    // 比例常数（精确到小数点后6位）
    double CENTER_X = centerX;
    double CENTER_Y = centerY;
    double  DIAL_RADIUS = dialRadius;
    const double SCALE_MAIN_OFFSET = DIAL_RADIUS * 0.666667;  // 120/180
    const double POINTER_LENGTH = DIAL_RADIUS * 0.555556;      // 100/180
    const double POINTER_WIDTH = DIAL_RADIUS * 0.055556;       // 10/180
    const double ROOT_OFFSET = DIAL_RADIUS * 0.055556;         // 10/180

    // 参数计算
    const double baseX = CENTER_X + SCALE_MAIN_OFFSET;
    const double baseY = CENTER_Y;

    // 角度计算（增加180度偏移）
    const double angle = -64 + (right / 2.744) * 5 - 180.0;
    const double radian = angle * M_PI / 180.0;

    // 方向向量
    const double dirX = cos(radian);
    const double dirY = sin(radian);
    const double perpX = -dirY;
    const double perpY = dirX;

    // 顶点坐标计算
    ege_point points[4] = {
        { // 尖端
            round(baseX + POINTER_LENGTH * dirX),
            round(baseY + POINTER_LENGTH * dirY)
        },
        { // 根部左侧
            round(baseX - POINTER_WIDTH * perpX),
            round(baseY - POINTER_WIDTH * perpY)
        },
        { // 根部中心
            round(baseX - ROOT_OFFSET * dirX),
            round(baseY - ROOT_OFFSET * dirY)
        },
        { // 根部右侧
            round(baseX + POINTER_WIDTH * perpX),
            round(baseY + POINTER_WIDTH * perpY)
        }
    };

    setfillcolor(EGERGB(210, 210, 210));
    ege_fillpoly(4, points);
}
